package com.kitty.game.house;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.house.message.*;
import com.kitty.game.house.model.House;
import com.kitty.game.house.service.HouseService;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.NotifyModules;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;

@Controller
public class HouseController {

    Logger logger = LoggerFactory.getLogger(HouseController.class);
    static CountService countService = (CountService) SpringUtils.getBean(CountService.class);
    @Autowired
    protected HouseService houseService;

    /*******************  居所 模块 ***************************/


    /**
     * 摆放家具
     * @param session
     * @param reqHousePlaceFurniture
     */
    @RequestMapping
    public void reqHousePlaceFurniture(IoSession session, ReqHousePlaceFurniture reqHousePlaceFurniture) {
        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
        int pos = reqHousePlaceFurniture.getPos();

        houseService.placeFurniture(role, reqHousePlaceFurniture);
    }


    /**
     * 购买家具
     * @param session
     * @param reqHouseBuyFurniture
     */
    @RequestMapping
    public void reqHouseBuyFurniture(IoSession session, ReqHouseBuyFurniture reqHouseBuyFurniture) {
        //4D 5A 00 00 00 00 00 00 00 0F FF F7 00 02 53 F4 00 01 00 1B 03 03 3E 13 FC //扣除金钱

        //4D 5A 00 00 00 00 00 00 00 75 F0 ED MSG_STORE = 61677;//F0 ED仓库
        // 0F 66 75 72 6E 69 74 75 72 65 5F 73 74 6F 72 65 00 00 00 00 00 01 01 0B BE 00 01 00 01 00 0C 00 CE 03 00 00 00 04 01 4F 02 00 00 00 4A 07 00 12 00 28 07 27 20 00 CF 03 00 0F 42 40 00 01 04 0C D4 C2 BC BE A3 A8 BB A8 C5 E8 A3 A9 00 D1 04 04 BD F0 C9 AB 00 54 03 00 03 8E B7 01 0E 03 00 00 00 02 00 CB 02 00 01 00 2A 03 FF FF FF FF 01 1A 02 00 01

        //4D 5A 00 00 00 00 00 00 00 4A 50 01 00 42 C4 E3 BB A8 B7 D1 23 52 23 63 42 41 35 35 44 43 33 2C 30 30 30 2C 30 30 30 23 6E CE C4 C7 AE 23 6E 23 6E B9 BA C2 F2 C1 CB 23 52 31 23 6E C5 E8 23 52 D4 C2 BC BE A3 A8 BB A8 C5 E8 A3 A9 23 6E A1 A3 62 57 04 1B
        //你花费#R#cBA55DC3,000,000#n文钱#n#n购买了#R1#n盆#R月季（花盆）#n。
        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
        houseService.buyFurniture(role, reqHouseBuyFurniture.getFurnitureId(),reqHouseBuyFurniture.getNum());
    }

    @RequestMapping
    public void reqReqCacheAutoWalkMsg(IoSession session, ReqCacheAutoWalkMsg reqCacheAutoWalkMsg) {

        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
        SpringUtils.getMapService().changeMap(role, 5000, 223,168);

    }

    /**
     * 请求样板型家具
     * @param session
     * @param reqHouseTemplateList
     */
    @RequestMapping
    public void reqHouseTemplateList(IoSession session, ReqHouseTemplateList reqHouseTemplateList) {

        //MSG_HOUSE_TEMPLATE_LIST 协议内容不全

        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
//        RespHouseFunctionFurnitureList respHouseFunctionFurnitureList = new RespHouseFunctionFurnitureList();
//        respHouseFunctionFurnitureList.setHouseId(role.getHouseId());
//        respHouseFunctionFurnitureList.setFunctionFurnitureList(new ArrayList<>());
//        MessagePusher.pushMessage(role, respHouseFunctionFurnitureList);
    }

    /**
     * 请求功能型家具
     */
    @RequestMapping
    public void reqHouseFunctionFurnitureList(IoSession session, ReqHouseFunctionFurnitureList reqHouseFunctionFurnitureList) {

        //MSG_HOUSE_FUNCTION_FURNITURE_LIST

        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
        RespHouseFunctionFurnitureList respHouseFunctionFurnitureList = new RespHouseFunctionFurnitureList();
        respHouseFunctionFurnitureList.setHouseId(role.getExtendBox().getHouseId());
        respHouseFunctionFurnitureList.setFunctionFurnitureList(new ArrayList<>());
        MessagePusher.pushMessage(role, respHouseFunctionFurnitureList);
    }

    /**
     * 开始管理房屋
     * @param session
     * @param reqHouseTryManage
     */
    @RequestMapping
    public void reqHouseTryManage(IoSession session, ReqHouseTryManage reqHouseTryManage) {

        Role role = SessionUtils.getRoleBySession(session);
        if(role ==null){
            return;
        }
        //MSG_HOUSE_DATA = 20594;                     //返回居所信息
        House house = role.getExtendBox().getHouse();
        RespHouseData respHouseData = new RespHouseData();
        respHouseData.setHouseId(house.getHouseId());
        respHouseData.setHouseType(house.getHouseType());
        respHouseData.setHousePrefix(role.getName());
        respHouseData.setComfort(house.getComfort());
        respHouseData.setCleanliness(house.getCleanliness());
        respHouseData.setCleanCostTime(house.getCleanCostTime());
        respHouseData.setStoreType(house.getStoreType());
        MessagePusher.pushMessage(role, respHouseData);

        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_OPEN_DLG); //对话框
        respGeneralNotify.setValue("HomePuttingDlg");
        MessagePusher.pushMessage(role, respGeneralNotify);

    }

    /**
     * 请求居所信息 主界面打开居所
     * @param role
     * @param reqHouseData
     */
    @RequestMapping
    public void reqHouseData(Role role, ReqHouseData reqHouseData) {

        logger.debug("role_name:"+role.getName()+",请求居所信息:" + reqHouseData.toString());

//        RespHouseData respHouseData = new RespHouseData();
//        respHouseData.setHouseId(role.getRoleId()+"");
//        respHouseData.setHouseType((byte) 1);
//        respHouseData.setHousePrefix(role.getName());
//        respHouseData.setComfort((short) 10);
//        respHouseData.setCleanliness((byte) 10);
//        respHouseData.setCleanCostTime((byte) 0);
//        respHouseData.setStoreType((byte) 1);
//        MessagePusher.pushMessage(role, respHouseData);
        House house = role.getExtendBox().getHouse();
        RespHouseData respHouseData = new RespHouseData();
        respHouseData.setHouseId(house.getHouseId());
        respHouseData.setHouseType(house.getHouseType());
        respHouseData.setHousePrefix(role.getName());
        respHouseData.setComfort(house.getComfort());
        respHouseData.setCleanliness(house.getCleanliness());
        respHouseData.setCleanCostTime(house.getCleanCostTime());
        respHouseData.setStoreType(house.getStoreType());
        MessagePusher.pushMessage(role, respHouseData);

        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_OPEN_DLG); //对话框
        respGeneralNotify.setValue(reqHouseData.getDlg());
        MessagePusher.pushMessage(role, respGeneralNotify);
    }

    /**
     * 购买居所
     * @param role
     * @param reqBuyHouse
     */
    @RequestMapping
    public void reqBuyHouse(Role role, ReqBuyHouse reqBuyHouse) {

        logger.debug("role_name:"+role.getName()+",居所信息:" + reqBuyHouse.toString());

        House house =new House();
        house.setHouseId("HS"+role.getRoleId());
        house.setRoleId(role.getRoleId());
        house.setHouseName(reqBuyHouse.getHouseName());
        house.setBedroomLevel(reqBuyHouse.getBedroomLevel());
        house.setStoreLevel(reqBuyHouse.getStoreLevel());
        house.setLianqsLevel(reqBuyHouse.getLianqsLevel());
        house.setXiuliansLevel(reqBuyHouse.getXiuliansLevel());
        house.setHousePrefix(role.getName());

//        xiaoshe = 1,  -- 小舍
//        yazhu = 2,    -- 雅筑
//        haozhai = 3,  -- 豪宅
        int price = 0;
        if(reqBuyHouse.getHouseName().equals("小舍")){
            house.setHouseType((byte) 1);
            price = 10000000;
        }
        if(reqBuyHouse.getHouseName().equals("雅筑")){
            house.setHouseType((byte) 2);
            price = 50000000;
        }
        if(reqBuyHouse.getHouseName().equals("豪宅")){
            house.setHouseType((byte) 3);
            price = 100000000;
        }
        house.setPrice(price);

        houseService.buyHouse(role, house);

    }

//    /**
//     * 请求养育娃娃信息
//     * @param role
//     * @param reqChildRequestInfo
//     */
//    @RequestMapping
//    public void reqChildRequestInfo(Role role, ReqChildRequestInfo reqChildRequestInfo) {
//
//        logger.debug("role_name:"+role.getName()+",居所信息:");
//        RespChildInfo respChildInfo =new RespChildInfo();
//        respChildInfo.setIsForceOpen((byte) 0);
//        respChildInfo.setSelectId("");
//
//        MessagePusher.pushMessage(role, respChildInfo);
//    }

    /**
     * 进入居所
     * @param role
     * @param reqHouseGoHome
     */
    @RequestMapping
    public void reqHouseGoHome(Role role, ReqHouseGoHome reqHouseGoHome) {

        logger.debug("role_name:"+role.getName()+",居所信息:");

//        RespSwitchServer respSwitchServer = new RespSwitchServer();
//        respSwitchServer.setResult((short) 1);
//        respSwitchServer.setContent("");
//
//
//        MessagePusher.pushMessage(role, respSwitchServer);

        if(StringUtils.isBlank(role.getExtendBox().getHouseId())){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("无居所信息"));
            return;
        }
        int mapId = 28101;
//        if(role.getHouse().getHouseType() == (byte) 2){
//            mapId = 28201;
//        }
//        if(role.getHouse().getHouseType() == (byte) 3){
//            mapId = 28301;
//        }

        House house = role.getExtendBox().getHouse();
        RespHouseData respHouseData = new RespHouseData();
        respHouseData.setHouseId(house.getHouseId());
        respHouseData.setHouseType((byte)1);
        respHouseData.setHousePrefix("小舍");
        respHouseData.setComfort(house.getComfort());
        respHouseData.setCleanliness(house.getCleanliness());
        respHouseData.setCleanCostTime(house.getCleanCostTime());
        respHouseData.setStoreType(house.getStoreType());
        MessagePusher.pushMessage(role, respHouseData);

        ReqHouseAutoWalk reqHouseAutoWalk = new ReqHouseAutoWalk();
        reqHouseAutoWalk.setA(role.getExtendBox().getHouseId());
        reqHouseAutoWalk.setB(house.getHouseName());
        MessagePusher.pushMessage(role, reqHouseAutoWalk);
        RespHouseFurnitureData respHouseFurnitureData = new RespHouseFurnitureData();
        respHouseFurnitureData.setHouseId(role.getExtendBox().getHouseId());
        MessagePusher.pushMessage(role, respHouseFurnitureData);
        SpringUtils.getMapService().changeMap(role, mapId, 40,30);
    }
    /*******************  居所 结束 ***************************/


}
